﻿Imports Microsoft.VisualBasic
Imports System
Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Data
Imports MySql.Data
Imports DevExpress.Utils
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid.Views.Card
Imports DevExpress.XtraGrid.Views.Base.BaseView
Imports DevExpress.XtraEditors
Imports DevExpress.XtraEditors.Repository
Imports DevExpress.XtraTab
Imports System.IO
Imports DevExpress.XtraGrid.Views.Base

Public Class FormDataPribadi
    Inherits PageTemplate
    Dim cani As Boolean = False
    Dim canu As Boolean = False
    Dim cand As Boolean = False

    Private constparam() As String = {"nis", "nama", "alamat", "kota", "kodepos", "jeniskelamin", "tempatlahir", "tanggallahir", "notelp", "namaayah", "namaibu", "catatan", "foto"}
    Private Sub LoadDataSiswa(ByRef dtsiswa As DataTable)
        Dim strQuery As String()
        Dim err As String = ""
        strQuery = New String() {"*", "datasiswa", "nis", ""}
        If Utils.executeSP("proc_searchall", strQuery, dtsiswa) Then
            If Utils.RowExist(dtsiswa) Then
                GridControl1.DataSource = dtsiswa
                GridControl1.Refresh()
                GridControl1.RefreshDataSource()

                GridView1.RefreshData()
            End If
        End If
    End Sub

    Private Sub RefreshGrid()
        Dim dtsiswa As New DataTable
        LoadDataSiswa(dtsiswa)
        Dim item As New RepositoryItemPictureEdit()
        item.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Squeeze
        GridControl1.RepositoryItems.Add(item)
        If dtsiswa.Rows.Count > 0 Then
            GridView1.Columns("foto").ColumnEdit = item
        End If
        GridView1.OptionsBehavior.Editable = False
        GridView1.OptionsView.ShowGroupPanel = False
    End Sub
    

    Private Sub FormDataPribadi_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        RefreshGrid()
        cbTipeCari.SelectedIndex = 0
        Dim dr() As DataRow
        'Dim dt As New DataTable
        If Utils.RowExist(LocalSession.dtrole) Then
            dr = LocalSession.dtrole.Select("menuid = '" & LocalSession.PublicMenuId & "'")
            If dr.Length > 0 Then
                '       dt = dr.CopyToDataTable
                cani = IIf(IsDBNull(dr(0)("to_insert")), 0, dr(0)("to_insert"))
                canu = IIf(IsDBNull(dr(0)("to_update")), 0, dr(0)("to_update"))
                cand = IIf(IsDBNull(dr(0)("to_delete")), 0, dr(0)("to_delete"))
            End If
        End If
        btnTambah.Enabled = cani
        btnEdit.Enabled = canu
        btnHapus.Enabled = cand
    End Sub

    Public Sub Byte2Image(ByRef NewImage As Image, ByVal ByteArr() As Byte)
        '
        Dim ImageStream As MemoryStream
        Try

            If ByteArr.GetUpperBound(0) > 0 Then
                ImageStream = New MemoryStream(ByteArr)
                NewImage = Image.FromStream(ImageStream)
            Else
                NewImage = Nothing
            End If

        Catch ex As Exception

            NewImage = Nothing

        End Try

    End Sub

    Private Sub GridView1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GridView1.DoubleClick
        'Dim dlg As FormEditDataSiswa = New FormEditDataSiswa()
        'Dim i As Integer
        'Dim obj As DataRow
        'Dim nis As String = ""
        'Dim nama As String = ""
        'Dim alamat As String = ""
        'Dim kota As String = ""
        'Dim kodepos As String = ""
        'Dim jeniskelamin As String = ""
        'Dim tempatlahir As String = ""
        'Dim tanggallahir As String = ""
        'Dim notelp As String = ""
        'Dim namaayah As String = ""
        'Dim namaibu As String = ""
        'Dim catatan As String = ""

        'Dim imagebaru As Image
        'imagebaru = Nothing
        'If GridView1.RowCount > 0 Then
        '    i = GridView1.FocusedRowHandle
        '    obj = GridView1.GetDataRow(i)
        '    nis = obj("nis").ToString
        '    nama = obj("nama").ToString
        '    alamat = obj("alamat").ToString
        '    kota = obj("kota").ToString
        '    kodepos = obj("kodepos").ToString
        '    jeniskelamin = obj("jeniskelamin").ToString
        '    tempatlahir = obj("tempatlahir").ToString
        '    tanggallahir = obj("tanggallahir").ToString
        '    notelp = obj("notelp").ToString
        '    namaayah = obj("namaayah").ToString
        '    namaibu = obj("namaibu").ToString
        '    catatan = obj("catatan").ToString
        '    Byte2Image(imagebaru, obj("foto"))
        'End If
        'dlg.txtNIS.Text = nis
        'dlg.txtNAMA.Text = nama
        'dlg.txtKODEPOS.Text = kodepos
        'dlg.meALAMAT.Text = alamat
        'dlg.txtNOTELP.Text = notelp
        'dlg.txtTEMPATLAHIR.Text = tempatlahir
        'dlg.cbJNSKELAMIN.Text = jeniskelamin
        'dlg.txtNAMAAYAH.Text = namaayah
        'dlg.txtNAMAIBU.Text = namaibu
        'dlg.txtKOTA.Text = kota
        'dlg.deTANGGALLAHIR.Text = tanggallahir
        'dlg.meCATATAN.Text = catatan
        'dlg.ImageEdit1.Image = imagebaru

        'If dlg.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
        'End If
    End Sub

    Private Sub btnTambah_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTambah.Click
        Dim dlg As New FormEditSiswa
        dlg.exec = "tambah"        
        dlg.ClearInput()
        dlg.ShowDialog()
        RefreshGrid()

    End Sub
    
    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        Dim dlg As FormEditSiswa = New FormEditSiswa()
        Dim i As Integer
        Dim obj As DataRow

        Dim imagebaru As Image
        imagebaru = Nothing

        If GridView1.RowCount > 0 Then
            i = GridView1.FocusedRowHandle
            obj = GridView1.GetDataRow(i)
            
            For Each sctl As String In constparam
                Dim ctl As Control()
                
                ctl = dlg.Controls.Find("txt" & sctl.ToUpper, True)
                If ctl Is Nothing Or ctl.Length = 0 Then
                    ctl = dlg.Controls.Find("me" & sctl.ToUpper, True)
                    If ctl Is Nothing Or ctl.Length = 0 Then
                        ctl = dlg.Controls.Find("de" & sctl.ToUpper, True)
                        If ctl Is Nothing Or ctl.Length = 0 Then
                            If sctl = "jeniskelamin" Then
                                sctl = "jnskelamin"
                                ctl = dlg.Controls.Find("cb" & sctl.ToUpper, True)
                                sctl = "jeniskelamin"
                            Else
                                ctl = dlg.Controls.Find("cb" & sctl.ToUpper, True)
                            End If

                            If ctl Is Nothing Or ctl.Length = 0 Then
                                ctl = dlg.Controls.Find("ImageEdit1", True)
                            End If
                        End If
                    End If
                End If
                If ctl IsNot Nothing And ctl.Length > 0 Then
                    If TypeOf ctl(0) Is MemoEdit Then
                        Dim ctltxt As New MemoEdit
                        ctltxt = CType(ctl(0), MemoEdit)
                        Dim richEdit As New RichTextBox
                        richEdit.Text = IIf(IsDBNull(obj(sctl)), "", obj(sctl))
                        ctltxt.Lines = richEdit.Lines
                    ElseIf TypeOf ctl(0) Is TextEdit Then
                        Dim ctltxt As New TextEdit
                        ctltxt = CType(ctl(0), TextEdit)
                        ctltxt.Text = IIf(IsDBNull(obj(sctl)), "", obj(sctl).ToString)

                    ElseIf TypeOf ctl(0) Is ComboBoxEdit Then
                        Dim ctltxt As New ComboBoxEdit
                        ctltxt = CType(ctl(0), ComboBoxEdit)
                        ctltxt.Text = IIf(IsDBNull(obj(sctl)), "", obj(sctl).ToString)
                    ElseIf TypeOf ctl(0) Is DateEdit Then
                        Dim ctltxt As New DateEdit
                        ctltxt = CType(ctl(0), DateEdit)
                        ctltxt.DateTime = IIf(IsDBNull(obj(sctl)), "", obj(sctl).ToString)
                    ElseIf TypeOf ctl(0) Is PictureBox Then
                        Dim ctltxt As New PictureBox
                        ctltxt = CType(ctl(0), PictureBox)
                        If Not IsDBNull(obj(sctl)) Then
                            Byte2Image(imagebaru, obj(sctl))
                        End If
                        ctltxt.Image = imagebaru
                    End If
                End If
            Next

        End If

        dlg.exec = "edit"
        dlg.ShowDialog()
        RefreshGrid()
    End Sub

    Private Sub btnHapus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHapus.Click
        Dim dlg As FormEditSiswa = New FormEditSiswa()
        Dim i As Integer
        Dim obj As DataRow

        Dim imagebaru As Image
        imagebaru = Nothing

        If GridView1.RowCount > 0 Then
            i = GridView1.FocusedRowHandle
            obj = GridView1.GetDataRow(i)
            i = GridView1.FocusedRowHandle
            obj = GridView1.GetDataRow(i)

            For Each sctl As String In constparam
                Dim ctl As Control()

                ctl = dlg.Controls.Find("txt" & sctl.ToUpper, True)
                dlg.btnSimpan.Text = "Hapus"
                If ctl Is Nothing Or ctl.Length = 0 Then
                    ctl = dlg.Controls.Find("me" & sctl.ToUpper, True)
                    If ctl Is Nothing Or ctl.Length = 0 Then
                        ctl = dlg.Controls.Find("de" & sctl.ToUpper, True)
                        If ctl Is Nothing Or ctl.Length = 0 Then
                            If sctl = "jeniskelamin" Then
                                sctl = "jnskelamin"
                                ctl = dlg.Controls.Find("cb" & sctl.ToUpper, True)
                                sctl = "jeniskelamin"
                            Else
                                ctl = dlg.Controls.Find("cb" & sctl.ToUpper, True)
                            End If

                            If ctl Is Nothing Or ctl.Length = 0 Then
                                ctl = dlg.Controls.Find("ImageEdit1", True)
                            End If
                        End If
                    End If
                End If
                If ctl IsNot Nothing And ctl.Length > 0 Then
                    If TypeOf ctl(0) Is MemoEdit Then
                        Dim ctltxt As New MemoEdit
                        ctltxt = CType(ctl(0), MemoEdit)
                        Dim richEdit As New RichTextBox
                        richEdit.Text = IIf(IsDBNull(obj(sctl)), "", obj(sctl))
                        ctltxt.Lines = richEdit.Lines
                    ElseIf TypeOf ctl(0) Is TextEdit Then
                        Dim ctltxt As New TextEdit
                        ctltxt = CType(ctl(0), TextEdit)
                        ctltxt.Text = IIf(IsDBNull(obj(sctl)), "", obj(sctl).ToString)

                    ElseIf TypeOf ctl(0) Is ComboBoxEdit Then
                        Dim ctltxt As New ComboBoxEdit
                        ctltxt = CType(ctl(0), ComboBoxEdit)
                        ctltxt.Text = IIf(IsDBNull(obj(sctl)), "", obj(sctl).ToString)
                    ElseIf TypeOf ctl(0) Is DateEdit Then
                        Dim ctltxt As New DateEdit
                        ctltxt = CType(ctl(0), DateEdit)
                        ctltxt.DateTime = IIf(IsDBNull(obj(sctl)), "", obj(sctl).ToString)
                    ElseIf TypeOf ctl(0) Is PictureBox Then
                        Dim ctltxt As New PictureBox
                        ctltxt = CType(ctl(0), PictureBox)
                        If Not IsDBNull(obj(sctl)) Then
                            Byte2Image(imagebaru, obj(sctl))
                        End If
                        ctltxt.Image = imagebaru
                    End If
                End If
            Next
        End If
        
        dlg.exec = "hapus"
        dlg.ShowDialog()
        RefreshGrid()
    End Sub

    Private Sub SimpleButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton1.Click
        Dim strQuery As String()
        Dim err As String = ""
        Dim dtsiswa As New DataTable
        GridControl1.DataSource = Nothing
        If cbTipeCari.Text = "NIS" Then
            strQuery = New String() {"*", "datasiswa", "nis", txtCari.Text}
            If Utils.executeSP("proc_searchallEqual", strQuery, dtsiswa) Then
                If Utils.RowExist(dtsiswa) Then
                    GridControl1.DataSource = dtsiswa

                    Dim item As New RepositoryItemPictureEdit()
                    item.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Squeeze
                    GridControl1.RepositoryItems.Add(item)
                    If dtsiswa.Rows.Count > 0 Then
                        GridView1.Columns("foto").ColumnEdit = item
                    End If
                    GridView1.OptionsBehavior.Editable = False
                    GridView1.OptionsView.ShowGroupPanel = False

                    GridControl1.Refresh()
                    GridControl1.RefreshDataSource()
                    GridView1.RefreshData()
                Else
                    MsgBox("Maaf Data Siswa Dengan " & cbTipeCari.Text & " = " & txtCari.Text & _
                           " Tidak Terdaftar", MsgBoxStyle.Exclamation, "Cari Data Siswa")
                End If
            End If
        ElseIf cbTipeCari.Text = "Nama" Then
            strQuery = New String() {"*", "datasiswa", "nama", txtCari.Text}
            If Utils.executeSP("proc_searchall", strQuery, dtsiswa) Then
                If Utils.RowExist(dtsiswa) Then
                    GridControl1.DataSource = dtsiswa

                    Dim item As New RepositoryItemPictureEdit()
                    item.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Squeeze
                    GridControl1.RepositoryItems.Add(item)
                    If dtsiswa.Rows.Count > 0 Then
                        GridView1.Columns("foto").ColumnEdit = item
                    End If
                    GridView1.OptionsBehavior.Editable = False
                    GridView1.OptionsView.ShowGroupPanel = False

                    GridControl1.Refresh()
                    GridControl1.RefreshDataSource()
                    GridView1.RefreshData()
                Else
                    MsgBox("Maaf Data Siswa Dengan " & cbTipeCari.Text & " = " & txtCari.Text & _
                           " Tidak Terdaftar", MsgBoxStyle.Exclamation, "Cari Data Siswa")
                End If
            End If
        End If
    End Sub

    Private Sub txtCari_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCari.TextChanged
        Dim strQuery As String()
        Dim err As String = ""
        Dim dtsiswa As New DataTable
        GridControl1.DataSource = Nothing
        If cbTipeCari.Text = "NIS" Then
            strQuery = New String() {"*", "datasiswa", "nis", txtCari.Text}
            If Utils.executeSP("proc_searchallEqual", strQuery, dtsiswa) Then
                If Utils.RowExist(dtsiswa) Then
                    GridControl1.DataSource = dtsiswa

                    Dim item As New RepositoryItemPictureEdit()
                    item.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Squeeze
                    GridControl1.RepositoryItems.Add(item)
                    If dtsiswa.Rows.Count > 0 Then
                        GridView1.Columns("foto").ColumnEdit = item
                    End If
                    GridView1.OptionsBehavior.Editable = False
                    GridView1.OptionsView.ShowGroupPanel = False

                    GridControl1.Refresh()
                    GridControl1.RefreshDataSource()
                    GridView1.RefreshData()

                End If
            End If
        ElseIf cbTipeCari.Text = "Nama" Then
            strQuery = New String() {"*", "datasiswa", "nama", txtCari.Text}
            If Utils.executeSP("proc_searchall", strQuery, dtsiswa) Then
                If Utils.RowExist(dtsiswa) Then
                    GridControl1.DataSource = dtsiswa

                    Dim item As New RepositoryItemPictureEdit()
                    item.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Squeeze
                    GridControl1.RepositoryItems.Add(item)
                    If dtsiswa.Rows.Count > 0 Then
                        GridView1.Columns("foto").ColumnEdit = item
                    End If
                    GridView1.OptionsBehavior.Editable = False
                    GridView1.OptionsView.ShowGroupPanel = False

                    GridControl1.Refresh()
                    GridControl1.RefreshDataSource()
                    GridView1.RefreshData()
                End If
            End If
        End If
    End Sub

    Private Sub SimpleButton5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton5.Click
        RefreshGrid()
        cbTipeCari.SelectedIndex = 0
    End Sub

    Private Sub SimpleButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton2.Click
        Dim i As Integer = 0
        Dim obj As DataRow
        If GridView1.RowCount > 0 Then
            i = GridView1.FocusedRowHandle
            obj = GridView1.GetDataRow(i)
            LocalSession.NisDataKesehatan = IIf(IsDBNull(obj("nis")), "", obj("nis").ToString)
            If LocalSession.NisDataKesehatan = "" Then
                MsgBox("Maaf Data Siswa Tidak Valid", MsgBoxStyle.Exclamation)
            Else
                BaseMain.SetSelectMenu("FormDataKesehatan", "Data Kesehatan")
            End If
        End If        
    End Sub

    Private Sub SimpleButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton3.Click
        Dim i As Integer = 0
        Dim obj As DataRow
        If GridView1.RowCount > 0 Then
            i = GridView1.FocusedRowHandle
            obj = GridView1.GetDataRow(i)
            LocalSession.NisTesKesehatan = IIf(IsDBNull(obj("nis")), "", obj("nis").ToString)
            LocalSession.NamaTesKesehatan = IIf(IsDBNull(obj("nama")), "", obj("nama").ToString)
            If LocalSession.NisTesKesehatan = "" Then
                MsgBox("Maaf Data Siswa Tidak Valid", MsgBoxStyle.Exclamation)
            Else
                BaseMain.SetSelectMenu("FormTesKesehatan", "Data Tes Kesehatan")
            End If
        End If
    End Sub

    Private Sub SimpleButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton4.Click
        Dim i As Integer = 0
        Dim obj As DataRow
        If GridView1.RowCount > 0 Then
            i = GridView1.FocusedRowHandle
            obj = GridView1.GetDataRow(i)
            LocalSession.NisRiwKesehatan = IIf(IsDBNull(obj("nis")), "", obj("nis").ToString)
            LocalSession.NamaRiwKesehatan = IIf(IsDBNull(obj("nama")), "", obj("nama").ToString)
            If LocalSession.NisRiwKesehatan = "" Then
                MsgBox("Maaf Data Siswa Tidak Valid", MsgBoxStyle.Exclamation)
            Else
                BaseMain.SetSelectMenu("FormRiwayatKesehatan", "Data Riwayat Kesehatan")
            End If
        End If
    End Sub
End Class
